use ClientPlatform

function uuid() {
    function s4() {
        return Math.floor((1 + Math.random()) * 0x10000).toString(16).substring(1);
    }
    return s4() + s4() + "-" + s4() + "-" + s4() + "-" + s4() + "-" + s4() + s4() + s4();
}

function HexToBase64(hex) {
    var base64Digits = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
    var base64 = "";
    var group;

    for (var i = 0; i < 30; i += 6) {
        group = parseInt(hex.substr(i, 6), 16);
        base64 += base64Digits[(group >> 18) & 0x3f];
        base64 += base64Digits[(group >> 12) & 0x3f];
        base64 += base64Digits[(group >> 6) & 0x3f];
        base64 += base64Digits[group & 0x3f];
    }

    group = parseInt(hex.substr(30, 2), 16);
    base64 += base64Digits[(group >> 2) & 0x3f];
    base64 += base64Digits[(group << 4) & 0x3f];
    base64 += "==";

    return base64;
}

function NUUID(uuid) {
    var hex = uuid.replace(/[{}-]/g, ""); // remove extra characters
    var a = hex.substr(6, 2) + hex.substr(4, 2) + hex.substr(2, 2) + hex.substr(0, 2);
    var b = hex.substr(10, 2) + hex.substr(8, 2);
    var c = hex.substr(14, 2) + hex.substr(12, 2);
    var d = hex.substr(16, 16);
    hex = a + b + c + d;
    var base64 = HexToBase64(hex);

    return new BinData(3, base64);
}

//Hard-coded strings
var productInvolvement = db.CriteriaLevel2.findOne({Name: 'Product Involvement'});
var newPIIndicators = [ {Code:"OS", Name:"Oil Sands", Order:220}, 
                        {Code: "AC", Name:"Arctic Oil & Gas Exploration", Order:200}];
//appliesTo: ""=[both], 1=[OS1/AC1 only], 2=[OS2/AC2 2 only]
var templateSuffixes = [{AppliesTo:"", Name:"Standard Analysis", IsScreanable: false},
                        {AppliesTo:"", Name:"Category of Involvement", IsScreanable: false},
                        {AppliesTo:"1", Name:"Level of Involvement", IsScreanable: true},
                        {AppliesTo:"2", Name:"Range Ownership", IsScreanable: true}];
var categories = [{Code: "1", Name:"Extraction"}, 
                  {Code: "2", Name:"Significant ownership (extraction)"}];
	          
var criteriaType = 4;
var criteriaEntity = "ScreeningData";

if (productInvolvement) {
	newPIIndicators.forEach(function(piIndicator) {

        //Remove old CriteriaLeve3 if exists	 
        db.CriteriaLevel3.remove({ CriteriaLevel2Id: productInvolvement._id, Name: piIndicator.Name })

        //Add the new CriteriaLevel3 PI indicators     
        db.CriteriaLevel3.insert({
            _id: NUUID(uuid()),
            Name: piIndicator.Name,
            ShortName: '',
            Order: NumberInt(piIndicator.Order),
            CriteriaLevel2Id: productInvolvement._id,
            Category: 'None'
        })

        var piCritLvl3 = db.CriteriaLevel3.findOne({ CriteriaLevel2Id: productInvolvement._id, Name: piIndicator.Name });

        categories.forEach(function(category) {
            templateSuffixes.forEach(function(templateSuffix) {
                if (templateSuffix.AppliesTo == "" || templateSuffix.AppliesTo == category.Code)
                {
                    var tempCritName = piIndicator.Name + " " + category.Name + "-" + templateSuffix.Name;
                    //Remove old template criteria if exists                    
                    db.TemplateCriteria.remove({Name: tempCritName, CriteriaType: criteriaType});                    
                    
                    //Add the new template criteria
                    db.TemplateCriteria.insert({
                        _id: NUUID(uuid()),
                        Name: tempCritName,
                        CriteriaEntity: criteriaEntity,
                        CriteriaEntityField: piIndicator.Code + " " + piIndicator.Code + category.Code + " " + templateSuffix.Name,
                        CriteriaLevel2Id: piCritLvl3.CriteriaLevel2Id,
                        CriteriaLevel3Id: piCritLvl3._id,
                        CriteriaType: NumberInt(criteriaType),
                        IsScreenable: templateSuffix.IsScreanable
                    });
                }
            });
        });
    });
}